<template>
  <div >
    <a-row :gutter="10" type="flex" >
      <a-col flex="5">
          <a-form ref="formRef" :model="form" :label-col="{ span: 6 }">
    <a-form-item label="事件名称" name="name">
      <a-input v-model:value="form.event_name" placeholder="请输入事件名称" defaultValue="B737NG_7_右发引气压力高"  />
    </a-form-item>

    <a-form-item label="参数输入" name="event_param">
      <a-select v-model:value="form.event_param" mode="tags" style="width: 100%" placeholder="选择参数" :options="paramOptions" @change="handleChange"
></a-select>
    </a-form-item>
    
    <a-form-item >
      <a-list size="small" item-layout="horizontal" bordered name="event_param_list" :data-source="paramList">
        <template #renderItem="{ item }">
          <a-list-item>
            {{item.paramDesc}}
          </a-list-item>
        </template>
        <template #header>
      <div>已选参数</div>
    </template>
  </a-list>
    </a-form-item>
    <a-form-item label="预警等级" name="event_rank">
      <a-checkbox-group v-model:value="form.event_rank" :options="plainOptions" @change="rankChange" />
    </a-form-item>
    <a-form-item label="时间窗口" name="event_time_window">
      <a-input-group compact>
      <a-input v-model:value="form.event_time_window" placeholder="设置时间窗口" style="width: calc(100% - 100px)" disabled="true"/> 
      <a-button type="primary" @click="win_btn_click"	style="width: 90px;"	>设置</a-button>
    </a-input-group>
    <a-modal :visible="openWin" width="600px" title="设置窗口时间" @ok="openWinOk" @cancel="openWinClose" cancelText="清除时间窗口">
      <p>在航班结束前<a-input-number id="win_start" v-model:value="winStart" :min="1" :max="100" />
        秒到<a-input-number id="win_end" v-model:value="winEnd" :min="1" :max="100" />秒的时间范围</p>
    </a-modal>
    </a-form-item>
    <a-form-item label="前置条件" name="event_condition">
      <a-textarea v-model:value="form.event_condition" placeholder="请输入前置条件" rows="3" />
    </a-form-item>
    <a-form-item label="判断逻辑" name="event_judement">
      <a-input-group compact>
      <a-input v-model:value="form.event_judement" placeholder="请输入判断逻辑" style="width: calc(100% - 100px)" disabled="true" />
      <a-button type="primary" @click="jude_btn_click" style="width: 90px;"	>设置</a-button>
    </a-input-group>
    <a-modal :visible="openJude" width="800px" title="设置判断逻辑" @ok="openJudeOk" @cancel="openJudeClose" cancelText="清除判断逻辑">
      <p><a-radio-group v-model:value="judeChange" @change="judeChangeChk">
        <a-radio :value="1">配置区间</a-radio><a-radio :value="2">配置事件值</a-radio>
      </a-radio-group></p>
      <p v-if="rangeShow">
        触发条件'<a-select v-model:value="judeParamRank1" placeholder="选择参数" :options="judeParamRankOptions"></a-select>':
        比较上述四个振动值的最大值，确定整体的最大值(maxValue)，若 
        <a-input-number  v-model:value="seriesCnt1" :min="1" :max="100" :step="0.1" /> 
        <a-select v-model:value="judeParamSign1" placeholder="选择参数" :options="judeParamSignOptions"></a-select> maxValue 
        <a-select v-model:value="judeParamSign2" placeholder="选择参数" :options="judeParamSignOptions"></a-select> 
        <a-input-number  v-model:value="seriesCnt2" :min="1" :max="100" :step="0.1"  />，eventRank 设为 
        <a-select v-model:value="judeParamRank2" placeholder="选择参数" :options="judeParamRankOptions"></a-select>
      </p>
      <p v-if="frequencyShow">分析四个振动值（左发风扇`<a-select v-model:value="judeParam1" placeholder="选择参数" :options="judeParamOptions"></a-select>`、
        左发低压涡轮`<a-select v-model:value="judeParam2" placeholder="选择参数" :options="judeParamOptions"></a-select>`、
        右发风扇`<a-select v-model:value="judeParam3" placeholder="选择参数" :options="judeParamOptions"></a-select>`、
        右发低压涡轮`<a-select v-model:value="judeParam4" placeholder="选择参数" :options="judeParamOptions"></a-select>`）并确定各自的最大振动值及其发生时间，进而比较四个振动值的最大值，确定整体的最大值(maxValue)进行评估事件的预警等级。</p>
    </a-modal>
    </a-form-item>
    <a-form-item >
      <a-list size="small" bordered item-layout="horizontal" name="event_judement_list" :data-source="judeList">
        <template #renderItem="{ item }">
          <a-list-item>
            {{item.jude_value}}
          </a-list-item>
        </template>
        <template #header>
      <div>判断逻辑</div>
    </template>
  </a-list>
    </a-form-item>
    <a-form-item label="返回结果" name="event_return">
      <a-input-group compact>
      <a-textarea v-model:value="form.event_return" placeholder="请输入返回结果" :rows="2" disabled="true" defaultValue="" style="width: calc(100% - 100px)" />
      <a-button type="primary" @click="ret_btn_click" style="width: 90px;"	>设置</a-button>
    </a-input-group>
    <a-modal :visible="openRet" width="800px" title="设置判断逻辑" @ok="openRetOk" @cancel="openRetClose" cancelText="清除判断逻辑">
    <p>
      四个振动值各自的最大振动值，以及在达到最大值时对应的发生时间、引擎（`<a-select v-model:value="judeParam5" placeholder="选择参数" :options="judeParamOptions"></a-select>`、
      `<a-select v-model:value="judeParam6" placeholder="选择参数" :options="judeParamOptions"></a-select>`）
      的值和飞行阶段（`<a-select v-model:value="judeParam7" placeholder="选择参数" :options="judeParamOptions"></a-select>`）的值。预警等级以及预警时间、触发预警等级对应的飞行阶段的值</p>
    </a-modal>
    </a-form-item>
  </a-form>
      </a-col>
      <a-col flex="5">
          <a-textarea v-model:value="form.result" placeholder="请在左边设置参数后点击生成代码按钮生成" :rows="40" />
      </a-col>
    </a-row>
  </div>  
  <div style="margin-top: 10px;"> <a-row><a-button type="primary" @click="generate_btn_click" style="width: 100px;margin-left: 45%;"	>生成代码</a-button></a-row></div>
</template>

<script setup lang="ts">
import {reactive, ref } from 'vue';



const formDefault = {
  event_name: undefined,
  event_param: undefined,
  event_rank: undefined,
  event_time_window: undefined,
  event_condition: undefined,
  event_judement: undefined,
  event_return: undefined,
  result: undefined,
};
let form = reactive({ ...formDefault });
const paramList=ref([]);
const judeList=ref([]);

const openWin=ref(false);
const winStart=ref();
const winEnd=ref();

const judeChange=ref(1);
const rangeShow=ref(true)
const frequencyShow=ref(false)

const openJude=ref(false);
const judeParamOptions=ref([]);
const judeParamRankOptions=ref([]);

const judeParamRank1=ref();
const seriesCnt1=ref();
const judeParamSign1=ref();
const judeParamSign2=ref();
const seriesCnt2=ref();
const judeParamRank2=ref();

const judeParam1=ref();
const judeParam2=ref();
const judeParam3=ref();
const judeParam4=ref();

const judeParam5=ref();
const judeParam6=ref();
const judeParam7=ref();

const openRet=ref(false);

let returnVal={};
let paramRetValue=[];
let rankRetValue=[];
let judeRetValue=[];

const handleChange = (value: string) => {
  paramList.value=[];
  judeParamOptions.value=[];
  paramRetValue=[];
  if(Array.isArray(value)){
    value.forEach((key)=>{
      paramList.value.push({paramDesc:map.get(key)});
      judeParamOptions.value.push({value:key,label:key})
      paramRetValue.push(key);
    });
  }
};

const rankChange=(value:string)=>{
judeParamRankOptions.value=[];
rankRetValue=[];
if(Array.isArray(value)){
    value.forEach((key)=>{
      judeParamRankOptions.value.push({value:key,label:key})
      rankRetValue.push(key)
    });
  }
};

const judeChangeChk=(e:Event)=>{
let chkValue=e.target.value;
if(chkValue===1){
  rangeShow.value=true;
  frequencyShow.value=false;
}else{
  rangeShow.value=false;
  frequencyShow.value=true;
}
}


const win_btn_click=()=>{
openWin.value=true;
};

function openWinOk(){
form.event_time_window = `在航班结束前${winStart.value}秒到${winEnd.value}秒的时间范围`;
openWin.value=false;
}

function openWinClose(){
form.event_time_window='';
openWin.value=false;
}

function openJudeOk(){
if(judeChange.value===1){
var judeValue=`触发条件'${judeParamRank1.value}'：比较上述四个振动值的最大值，确定整体的最大值(maxValue)，若 ${seriesCnt1.value} ${judeParamSign1.value} maxValue ${judeParamSign2.value} ${seriesCnt1.value}，eventRank 设为 ${judeParamRank2.value}。`;
var conditionValue=[];

conditionValue.push(judeParamRank1.value);
conditionValue.push(seriesCnt1.value);
conditionValue.push(judeParamSign1.value);
conditionValue.push(judeParamSign2.value);
conditionValue.push(seriesCnt2.value);
conditionValue.push(judeParamRank2.value);

form.event_judement=judeValue;
judeList.value.push({jude_value:judeValue});
judeRetValue.push({'template':"触发条件'{}'：比较上述四个振动值的最大值，确定整体的最大值(maxValue)，若 {} {} maxValue {} {}，eventRank 设为 {}。","condition_value":conditionValue})
}else
{
  var judeValue=`分析四个振动值（左发风扇'${judeParam1.value}'、左发低压涡轮'${judeParam2.value}'、右发风扇'${judeParam3.value}'、右发低压涡轮'${judeParam4.value}'）并确定各自的最大振动值及其发生时间，进而比较四个振动值的最大值，确定整体的最大值(maxValue)进行评估事件的预警等级。`;
  var conditionValue=[];

  conditionValue.push(judeParam1.value);
  conditionValue.push(judeParam2.value);
  conditionValue.push(judeParam3.value);
  conditionValue.push(judeParam4.value);

  form.event_judement=judeValue;
  judeList.value.push({jude_value:judeValue});
  judeRetValue.push({'template':"分析四个振动值（左发风扇`{}`、左发低压涡轮`{}`、右发风扇`{}`、右发低压涡轮`{}`）并确定各自的最大振动值及其发生时间，进而比较四个振动值的最大值，确定整体的最大值(maxValue)进行评估事件的预警等级。","condition_value":conditionValue})
}
openJude.value=false;
}

function openJudeClose(){
  form.event_judement='';
  judeList.value=[];
  openJude.value=false;
}

function jude_btn_click(){
  openJude.value=true;
}

function ret_btn_click(){
  openRet.value=true;
}

function openRetOk(){
  var judeValue=`四个振动值各自的最大振动值，以及在达到最大值时对应的发生时间、引擎（'${judeParam5.value}'、'${judeParam6.value}'）的值和飞行阶段（'${judeParam7.value}'）的值。预警等级以及预警时间、触发预警等级对应的飞行阶段的值`;
  var conditionValue=[];

  conditionValue.push(judeParam5.value);
  conditionValue.push(judeParam6.value);
  conditionValue.push(judeParam7.value);

  form.event_return=judeValue;
  returnVal={'template':"四个振动值各自的最大振动值，以及在达到最大值时对应的发生时间、引擎（'{}'、'{}'）的值和飞行阶段（'{}'）的值。预警等级以及预警时间、触发预警等级对应的飞行阶段的值","condition_value":conditionValue};
  openRet.value=false;
}
function openRetClose(){
  form.event_return="";
  openRet.value=false;
}

function generate_btn_click(){
let retValue={"text": {"event_name": "","event_param": {"param":[],"frequency":[]},"event_rank": [],"event_condition": [],"event_time_window": {"template":"","param":[]},
"event_judgment": [{'template':"","condition_value":[]}],"event_return": {"template":"", "return_param":[]}}}

    let winParam=[];
    winParam.push(winStart.value);
    winParam.push(winEnd.value);

    let frequencyValue=[];
    paramRetValue.forEach((key)=>{
      frequencyValue.push(mapfrequency.get(key));
    });

    retValue.text.event_name=form.event_name;
    retValue.text.event_param.param=paramRetValue;
    retValue.text.event_param.frequency=frequencyValue;
    retValue.text.event_rank=rankRetValue;
    
    if(winStart.value==='' || winStart.value===undefined){
      retValue.text.event_time_window={};
    }else{
      retValue.text.event_time_window={"template":"在航班结束前的{}秒到{}秒的时间范围","param":winParam};
    }
    if(form.event_condition=='' || form.event_condition===undefined){
      retValue.text.event_condition=[];
    }
    else retValue.text.event_condition=[form.event_condition];
    
    retValue.text.event_judgment=judeRetValue;
    retValue.text.event_condition=form.event_condition;
    retValue.text.event_return=returnVal;

    form.result=JSON.stringify(retValue);

  }


const plainOptions=['CL0','CL1','CL2','CL3'];
const paramOptions=[{
      value:'VIB_N1FNT1',
      label:'VIB_N1FNT1'
    },
    {
      value:'VIB_N11',
      label:'VIB_N11'
    },
    {
      value:'N11_C',
      label:'N11_C'
    },
    {
      value:'VIB_N1FNT2',
      label:'VIB_N1FNT2'
    },
    {
      value:'VIB_N12',
      label:'VIB_N12'
    },
    {
      value:'N12_C',
      label:'N12_C'
    },
  {
    value:'OFMCUR',
    label:'OFMCUR'
  }];

  const judeParamSignOptions=[{
    value:'大于',
    label:'大于'
  },{
    value:'大于等于',
    label:'大于等于'
  },{
    value:'小于',
    label:'小于'
  },{
    value:'小于等于',
    label:'小于等于'
  }];

  const map: Map<string, string> = new Map([
['VIB_N1FNT1', '参数名称：VIB_N1FNT1，频率：1HZ'],
['VIB_N11', '参数名称：VIB_N11，频率：1HZ'],
['N11_C', '参数名称：N11_C，频率：1HZ'],
['VIB_N1FNT2', '参数名称：VIB_N1FNT2，频率：1HZ'],
['VIB_N12', '参数名称：VIB_N12，频率：1HZ'],
['N12_C', '参数名称：N12_C，频率：1HZ'],
['OFMCUR', '参数名称：OFMCUR，频率：1HZ'],
]);

const mapfrequency: Map<string, string> = new Map([
['VIB_N1FNT1', '1HZ'],
['VIB_N11', '1HZ'],
['N11_C', '1HZ'],
['VIB_N1FNT2', '1HZ'],
['VIB_N12', '1HZ'],
['N12_C', '1HZ'],
['OFMCUR', '1HZ'],
]);
</script>


<style scoped lang="less">
.height100 {
  height: 90%;
}
</style>